<?php
/**
 * 求购信息管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-07-26 
 */
namespace Admin\Controller;

class BuyController extends CommonController{
    /*
     * 构造函数
     * 
     * @return  #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db       =new \Model\BuyModel();
        $this->db_mes   =D("Comment");
    }    
    
    /*
     * 已审核的求购信息
     * 
     * @return #
     */
    public function index(){
        if(I("request.title")){
            $map['title']   =I("request.title");
        }
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map    =$this->_search("Buy");
        if(I("request.nickName")){
            $map['publisherId']=A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        if(I("request.regionId")){
            $map['regionId']    =I("request.regionId");
        }
        if(session('type')<2){
            $map['publisherId'] =session("authId");
        }
        $map['state']   =2;
        $sortBy         ="top";
        $sort           =desc;
        $infoList       =$this->_list($this->db,$map,$sortBy,$sort);
        $region         =D("Region")->select();
        $this->assign("regList",$region);
        $this->assign("infoList",$infoList);
        $this->assign("act",1);
        $this->display();
    }    
    
    /*
     * 待审核的求购信息
     * 
     * @return #
     */
    public function auditBuyInfo(){
        if(I("request.title")){
            $map['title']   =I("request.title");
        }
        if(I("request.content")){
            $map['content'] =I("request.content");
        }
        $map    =$this->_search("Buy");
        if(I("request.nickName")){
            $map['publisherId']=A("Common")->getIds(I("request.nickName"),"nickName","User");
        }
        if(I("request.regionId")){
            $map['regionId']    =I("request.regionId");
        }
        if(session('type')<2){
            $map['publisherId'] =session("authId");
        }
        if(I("request.publisherTel")){
            $map['publisherTel']=I("request.publisherTel");
        }
        $map['state']   =1;
        $sortBy         ="top";
        $sort           =desc;
        $infoList       =$this->_list($this->db,$map,$sortBy,$sort);        
        $region         =D("Region")->select();
        $this->assign("regList",$region);
        $this->assign("infoList",$infoList);
        $this->assign("act",2);
        $this->display("index");
    }
    
    /*
     * 显示编辑求购信息（包括新增和修改）
     * 
     * @return #
     */
    function show(){
        //$region =D("Region")->select();
        //$this->assign("region",$region);
        $provinceData = M('Area')->where("id like '__0000' and state='CN'")->select();
        $this->assign ('provinceData', $provinceData);
        if(I("request.id","","int")){
            $buyInfo=$this->db->find(I("request.id","","int"));
            $this->assign("buyInfo",$buyInfo);
            if (!empty($buyInfo['regionid'])){
                $areaInfo = M('Area')->where(array("id"=>$buyInfo['regionid']))->find();
            }
            if ($areaInfo){
                //获取城市
                $code = substr($areaInfo['id'], 0, 2);
                $cityData = M('Area')->where("id like '{$code}__00' and state='CN' and id<>'{$code}0000'")->select();
                $this->assign ('cityData', $cityData);
                //获取地区
                $code = substr($areaInfo['id'], 0, 4);
                $areaData = M('Area')->where("id like '{$code}__' and state='CN' and id<>'{$code}00'")->select();
                $this->assign ('areaData', $areaData);
                $this->assign('areaInfo', $areaInfo);
            }
        }
        $this->display();
    }
    
    /*
     * 审核批量审核信息
     * 
     * @return bool
     */
    public function alledit(){       
        $map['id']      =I("request.id");
        $map['state']   =2;
        if($this->db->edits($map)){            
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    
    /*
     * 待审核，批量待审核
     * 
     * @return #
     */
    public function audit(){
        $map['id']      =I("request.id");
        $map['state']   =1;
        if($this->db->edits($map)){            
            $this->success("待审核成功");
        }else{
            $this->error("待审核失败");
        }
    }
    
    /*
     * 删除，批量删除求购信息
     * 
     * @return #
     */
    public function del(){
        $map['id']      =I("request.id");
        $map['state']   =3;
        if($this->db->edits($map)){ 
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
    
    /*
     * 编辑求购信息，包括新增和修改
     * 
     * @return #
     */
    public function edit(){
        if(I("request.id")){
            $map['id']          =I("request.id",'','int');
        }else{            
            $map['publisherId'] =session('authId');
        }
        if(I("request.title")){
            $map['title']       =I("request.title",'','trim');
        }else{
            $this->error("求购主题不能为空");
        }
        if(I("request.content")){
            $map['content']     =I("request.content",'',"trim");
        }
        if(I("request.publisherAddr")){
            $map['publisherAddr']=I("request.publisherAddr",'',"trim");
        }
        if(I("request.publisherTel")){
            $map['publisherTel']=I("request.publisherTel");
        }else{
            $map['publisherTel']=session("tel");
        }
        if(I("request.top")){
            $map['top_sign']    =1;
        }
        if(I("request.province")){
            $map['regionId']    =I("post.province","","int");
        }
        if(I("request.city")){
            $map['regionId']    =I("post.city","","int");
        }
        if($this->db->buyEdit($map)){
            $this->success("求购信息编辑成功");
        }else{
            $this->error("求购信息编辑失败");
        }
    }
    
    /*
     * 留言列表
     * 
     * @return #
     */
    public function leavMessage(){
        $this->_search("LeavMessage");
        if(I("request.content")){
            $map['content'] =array("LIKE",'%'.trim(I("request.content")).'%');
        }
        if(I("request.add_time")){
            $map['commentTime'] =array("LIKE",'%'.trim(I("request.add_time")).'%');
        }
        $map['state']       =array("neq",3);
        //$map['forumType'] =4;
        $map['forumType']   =3;
        $list   =$this->_list($this->db_mes,$map);
        $this->assign("list",$list);
        $this->display();
    }
    
    /*
     * 显示新增留言
     * 
     * @return #
     */
    public function leavShow(){
        if(I("request.id")){
            $vo =$this->db_mes->find(I("request.id"));
            $this->assign("vo",$vo);
        }
        $this->display();
    }
    
    /*
     * 编辑留言
     * 
     * @return #
     */
    public function leaveEdit(){
        if(I("request.id")){
            //说明为修改
            if (false === $this->db_mes->create()) {
                $this->error($this->db_mes->getError());
            }
            if($this->db_mes->save()){
                $this->success("留言编辑成功");
            }else{
                $this->error("留言编辑失败");
            }
        }else{
            //为新新增
            if (false === $this->db_mes->create()) {
                $this->error($this->db_mes->getError());
            }
            if($this->db_mes->add()){
                $this->success("新增留言成功");
            }else{
                $this->error("新增留言失败");
            }
        }
    }
    
    /*
     * 删除留言
     * 
     * @return #
     */
    public function delLeavMes(){
        if(!I("request.id")){
            $this->error("参数错误");
        }
        if(is_array(I("request.id"))){
            $map['id']=array("in",implode(',',I("request.id")));            
        }else{
            $map['id']  =I("request.id");
        }
        $map['state']   =3;
        if($this->db_mes->save($map)){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
}
